﻿package com.content.model.LoadedAssets
{

	import com.content.view.Component.CButton.Level1VO;
	
	import flash.events.*;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.proxy.Proxy;
	import org.puremvc.as3.utilities.startupmanager.interfaces.IStartupProxy;
	
	public class PatternFeedProxy extends EntityProxy implements IStartupProxy
	{
		public static const NAME:String 						= "PatternFeedProxy";
        public static const SRNAME:String 						= "PatternFeedSRProxy";
		public static const LOAD_COMPLETE:String				= NAME + "-" + "LoadComplete";
		public static const LOAD_ERROR:String					= NAME + "-" + "LoadError";
		
		private var _pattern:Object;

		public function PatternFeedProxy() 
		{
			super( NAME, null);
			_pattern = new Object();
		}
		
		public function load() :void 
		{
//            sendNotification( AppFacade.CONFIG_LOADING );
			
			var request:URLRequest = new URLRequest("assets/xml/Pattern_Feed.xml");
			var loader:URLLoader = new URLLoader();
			
			loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
            loader.addEventListener(Event.COMPLETE, loaderCompleteHandler);
			
			loader.load(request);
        }
		
		private function loaderCompleteHandler(event:Event):void 
		{	
			var data:XML = new XML( event.target.data );
			// Feed List *************************
			var feedItem:XML;
			for each (feedItem in data.feedList.feed)
			{
				_pattern[feedItem.@ID] = {value:feedItem};
			}
			
			sendLoadedNotification( LOAD_COMPLETE, NAME, SRNAME );
		}
		
		private function errorHandler(e:IOErrorEvent):void 
		{
            sendLoadedNotification( LOAD_ERROR, NAME, SRNAME );
        }
		
		public function GetFeed(id:String):XML{
			
			var feedStr:String = _pattern[id]["value"]
			if (feedStr == null){
				trace(("No Feed for id: " + id));
				return null;
			};
			return (new XML(feedStr));
		}
	}
}